﻿using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;

namespace JYH.Framework
{
    class JyhtDynamicClass
    {
        /// <summary>
        /// 使用dynamic根据DataTable的列名自动添加属性并赋值
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns> 
        public static Object GetDynamicClassBydt(DataTable dt)
        {
            dynamic d = new System.Dynamic.ExpandoObject();
            //创建属性，并赋值。
            foreach (DataColumn cl in dt.Columns)
            {
                (d as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>(cl.ColumnName, dt.Rows[0][cl.ColumnName].ToString()));
            }
            return d;
        }
    }

   
    
}
